﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eProject_SEM3_SecurityServices.Bussiness;
using eProject_SEM3_SecurityServices.ObjectClass;
using System.Data.SqlClient;
using System.Data;
using eProject_SEM3_SecurityServices.DataAccess;

namespace eProject_SEM3_SecurityServices.Presentations
{
    public partial class frmManagerContract : System.Web.UI.Page
    {
        BContractDetail bcd = null;
        BContract bcontract = null;        
        BCustomer customer = null;
        BService bs = null;        
        bool visible = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            customer = new BCustomer();    
            bs = new BService();
            bcd = new BContractDetail();
            if (!IsPostBack)
            {                
                BindData();
                BindDataDdlService();
            }
            if (Request.QueryString["id"] != null)
            {
                int customerID = Convert.ToInt32(Request.QueryString["id"]);
                CustomerID.Text = customer.GetCustomerDetails(customerID).CustomerID.ToString();
                CustomerName.Text = customer.GetCustomerDetails(customerID).CustomerName;
                CustomerPhone.Text = customer.GetCustomerDetails(customerID).CustomerPhone;
                CustomerAddress.Text = customer.GetCustomerDetails(customerID).CustomerAddress;
                pnlNewContract.Visible = true;
            }
            else pnlNewContract.Visible = false;
        }
        protected void BindData()
        {           
            grvContract.DataSource = bcd.getContractDetailList();
            Label lblCount = new Label();
            //lblCount = (Label)grvContract.FooterRow.FindControl("lblCount");
            if (lblCount != null)
           {
                grvContract.DataSource = bcd.getContractDetailList();
                //Label lblCount = new Label();
                //lblCount = (Label)grvContract.FooterRow.FindControl("lblCount");
                if (lblCount != null)
                {
                    lblCount.Text = grvContract.Rows.Count.ToString();
                }

                grvContract.DataBind();

            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            OContractDetail contractdetail = new OContractDetail();
            int contractdetailId = bcd.getContractDetailList().Count() + 1;
            bcd = new BContractDetail();
            contractdetail.ContractId = contractdetailId;
            contractdetail.StartDay = txtStartDay.Text;
            contractdetail.EndDay = txtEndDay.Text;
            contractdetail.NumberEmployee = Convert.ToInt32(txtnumberEmp.Text.Trim());
            contractdetail.CustomerId = Convert.ToInt32(CustomerID.Text.Trim());
            contractdetail.RegistryDay = txtRegistryDay.Text.Trim();
            contractdetail.Status = ddlStatus.SelectedValue;
            contractdetail.ServiceId =int.Parse(ddlService.SelectedValue.ToString());
            bcd.AddNewContractDetail(contractdetail);           
            BindData();
        }
        public void BindDataDdlService()
        {
            ddlService.DataSource = bs.getServiceList();
            ddlService.DataTextField = "serviceName";
            ddlService.DataValueField = "serviceId";
            ddlService.DataBind();
        }

        protected void grvContract_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grvContract.EditIndex = -1;
            this.BindData();
        }

        protected void grvContract_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            OContractDetail contractDetail = new OContractDetail();
            try 
	        {	        
		        int id =int.Parse(grvContract.DataKeys[e.RowIndex].Value.ToString());
                TextBox txtNumOfEm = (TextBox)grvContract.Rows[e.RowIndex].FindControl("txtNumOfEm");
                TextBox txtRegistryDateE = (TextBox)grvContract.Rows[e.RowIndex].FindControl("txtRegistryDateE");               
                TextBox txtStartDateE = (TextBox)grvContract.Rows[e.RowIndex].FindControl("txtStartDateE");
                TextBox txtEndDateE = (TextBox)grvContract.Rows[e.RowIndex].FindControl("txtEndDateE");
                DropDownList ddlStatus = (DropDownList)grvContract.Rows[e.RowIndex].FindControl("ddlStatus");
                if (txtEndDateE == null || txtNumOfEm == null || txtRegistryDateE == null || txtStartDateE == null || ddlStatus == null)
                {
                    return;
                }
                contractDetail.ContractId = id;
                contractDetail.EndDay = txtEndDateE.Text.Trim();
                contractDetail.NumberEmployee = int.Parse(txtNumOfEm.Text.Trim());
                contractDetail.RegistryDay = txtRegistryDateE.Text.Trim();
                contractDetail.StartDay = txtStartDateE.Text.Trim();
                contractDetail.Status = ddlStatus.SelectedValue;
                if (bcd.updateContractDetail(contractDetail))
                {
                    Response.Write("<script>alert('Update successfull!')</script>");
                }
	        }
	        catch (Exception)
	        {
		
		        throw;
	        }
            grvContract.EditIndex = -1;
            this.BindData();
            
        }

        protected void grvContract_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grvContract.EditIndex = e.NewEditIndex;
            this.BindData();           
        }

        protected void grvContract_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

            int id = int.Parse(grvContract.DataKeys[e.RowIndex].Value.ToString());
            bcd.deleteContractDetail(id);
            BindData(); 
        }
       
        protected void ibtnRegistDay_Click1(object sender, ImageClickEventArgs e)
        {
            visible = !visible;
            cldRegistDay.Visible = visible;
        }

        protected void cldRegistDay_SelectionChanged1(object sender, EventArgs e)
        {
            string registDate = cldRegistDay.SelectedDate.ToShortDateString();
            txtRegistryDay.Text = registDate;
            visible = !visible;
            cldRegistDay.Visible = false;
        }

        protected void ibtnStartDay_Click1(object sender, ImageClickEventArgs e)
        {
            visible = !visible;
            cldStartDay.Visible = visible;
        }

        protected void cldStartDay_SelectionChanged1(object sender, EventArgs e)
        {
            string registDate = cldStartDay.SelectedDate.ToShortDateString();
            txtStartDay.Text = registDate;
            visible = !visible;
            cldStartDay.Visible = false;
        }

        protected void ibtnEndDay_Click1(object sender, ImageClickEventArgs e)
        {
            visible = !visible;
            cldEndDay.Visible = visible;
        }

        protected void cldEndDay_SelectionChanged1(object sender, EventArgs e)
        {
            string registDate = cldEndDay.SelectedDate.ToShortDateString();
            txtEndDay.Text = registDate;           
            cldEndDay.Visible = false;
        }

        protected void grvContract_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvContract.PageIndex = e.NewPageIndex;
            this.BindData();
        }                  

    }
}